import { getDevicesStates, selectForCloud } from '@/api/device'
import { mergeDevicesStates } from '@/utils/device'
import { getArrayItem, setArrayItem, convertArr2Obj } from '@/utils'
const prefix = 'intelirri-'

const getDefaultState = () => {
  return {
    currentDevice: null,
		currentSerialno:'',
    devicesObj: getArrayItem(`${prefix}devicesObj`),
    devices: getArrayItem(`${prefix}devices`),
    project: getArrayItem(`${prefix}project`),
		projects: getArrayItem(`${prefix}projects`),
		dclasses: getArrayItem(`${prefix}dclasses`),
    topics: getArrayItem(`${prefix}topics`),
    currentDeviceShow: false,
    rtk: { a: '', b: '', rtk: '', dir:'' }
  }
}

const state = getDefaultState()

const mutations = {
	SET_RTK: (state, rtk) => {
		state.rtk = rtk
	},
	RESET_STATE: (state) => {
	  Object.assign(state, getDefaultState())
	},
  SET_CURRENT_DEVICE: (state, currentDevice) => {
		state.currentDevice = currentDevice
		if(state.currentDevice){
			state.currentDevice = JSON.parse(JSON.stringify(state.currentDevice))
		}
  },
	SET_CURRENT_SERIALNO: (state, currentSerialno) => {
		state.currentSerialno = currentSerialno
	},
	SET_DEVICE: (state, device) => {
	  state.devicesObj[device.serialno] = device
	},
  SET_DEVICES_STORAGE: (state, devices) => {
		if(devices.length > 0){
			setArrayItem(`${prefix}devicesObj`, convertArr2Obj(devices,'serialno'))
			setArrayItem(`${prefix}devices`, devices)
		}
  },
  SET_DEVICES: (state, devices) => {
		if(devices.length > 0){
			state.devices = devices
			state.devicesObj = convertArr2Obj(devices,'serialno')
		}
  },
  SET_PROJECT: (state, project) => {
		if(project){
			setArrayItem(`${prefix}project`, project)
			state.project = project
		}
  },
	SET_PROJECTS: (state, projects) => {
		if(projects.length > 0){
			setArrayItem(`${prefix}projects`, projects)
			state.projects = projects
		}
	},
	SET_DCLASSES: (state, dclasses) => {
		if(dclasses){
			setArrayItem(`${prefix}dclasses`, dclasses)
			state.dclasses = dclasses
		}
	},
  SET_CURRENT_DEVICE_SHOW: (state, currentDeviceShow) => {
    state.currentDeviceShow = currentDeviceShow
  },
	SET_TOPICS: (state, topics) => {
		setArrayItem(`${prefix}topics`, topics)
		state.topics = topics
	},
}

const actions = {
  getAll({ commit }, params) {
		const project_id = params && params.project_id ? params.project_id : state.project && state.project.id ? state.project.id : null
		if(params){
			params.project_id = project_id
		}
    if (!state.devices || state.devices.length === 0 || params && params.refresh) {
			uni.showLoading({
				title:'加载中'
			})
      return new Promise((resolve, reject) => {
        selectForCloud(params).then(response => {
          const { data } = response
					// commit('SET_DEVICES_STORAGE', data.devices)
          commit('SET_DEVICES', data.devices)
          commit('SET_PROJECT', data.project)
					commit('SET_PROJECTS', data.projects)
					commit('SET_DCLASSES', data.dclasses)
          commit('SET_TOPICS', data.topics)
					uni.hideLoading()
          resolve(response)
        }).catch(error => {
					uni.hideLoading()
          reject(error)
        })
      })
    } else {
      return new Promise((resolve, reject) => {
        getDevicesStates({project_id:state.project.id}).then(response => {
          const { data } = response
          const devices = mergeDevicesStates(state.devices, data)
          commit('SET_DEVICES', devices)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
